#include "TurnsMenu.h"
#include "Utilities.h"
#include "PrintUtilities.h"
#include "User.h"
#include "ServiceProvider.h"
#include "League.h"
#include "LeagueManager.h"
#include "MenuManager.h"
#include "GamePrintUtils.h"

#include <algorithm>


TurnsMenu::TurnsMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
{
}

void TurnsMenu::OnEnter()
{
	m_selection = -1;
}

void TurnsMenu::Draw() const
{
	const LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);
	LeagueVecCit it = leagues.begin();
	LeagueVecCit end = leagues.end();

	size_t counter = 0;

	if (m_selection >= 0 && m_selection <= static_cast<int>(leagues.size()))
	{
		std::cout << Print::Title("THIS SEASON TURNS:");
		std::cout << "LEAGUE " << leagues.at(m_selection - 1)->GetName() << std::endl;
		PrintCalendar(std::cout, *leagues[m_selection - 1]);
		std::cout << std::endl;
	}

	std::cout << "Available leagues:" << std::endl;
	for (; it != end; ++it)
		std::cout << ++counter << ") " << (*it)->GetName() << std::endl;
	std::cout << std::endl;

	std::string options = "1-" + Utilities::IntToString(counter) + ") Select league.\n0) Return to previous menu'.";
	std::cout	<< Print::Options(options)
				<< "Selection: ";
}


bool TurnsMenu::Input(const std::string& i_input)
{
	const LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);
	bool goodInput = true;

	size_t res = Utilities::StringToInt(i_input);

	if (res == 0)
		GetMM()->SetMenu(GetMM()->GetMainMenu());
	else if (res > 0 && res <= leagues.size())
		m_selection = res;
	else
		goodInput = false;

	return goodInput;
}